#include "Dating.h"
Dating::Dating()
{
	
}
Dating::~Dating()
{
}
void Dating::printCoupleList()
{
	for (auto couple : coupleList)
	{
		couple.value.printCouple();
	}
}

bool Dating::matchPersons(int reqInterests)
{
	int matches = 0;
	int girlIndex = 0;
	for (auto girl : girlList)
	{
		int boyIndex = 0;
		matches = 0;
		for (auto boy : boyList)
		{
			for (size_t i = 0; i < girl.value.getInterests().size(); i++)
			{
				for (size_t j = 0; j < boy.value.getInterests().size(); j++)
				{
					if (girl.value.getInterest(i).compare(boy.value.getInterest(j)) == 0)
					{
						matches++;
						break;
					}
					if (matches >= reqInterests)
					{
						Couple c(girl.value, boy.value);
						int index = coupleListSorting(girl.value.getName());
						if (index == 0)
						{
							coupleList.push_front(c);
						}
						else
						{
							coupleList.insertAfter(c, index - 1);
						}
						
						if (girlIndex == 0)
						{
							girlList.pop_front();
						}
						else
						{
							girlList.eraseAfter(girlIndex);
						}
						if (boyIndex == 0)
						{
							boyList.pop_front();
						}
						else
						{
							boyList.eraseAfter(boyIndex);
						}
						return true;
					}
				}
			}
			boyIndex++;
		}
		girlIndex++;
	}
	return false;
}

int Dating::coupleListSorting(string name)
{
	int index = 0;
	if (coupleList.is_empty())
	{
		return index;
	}
	else
	{
		for (auto &couple : coupleList)
		{
			if (name.compare(couple.value.getGirlName()) < 0)
			{
				return index;
			}
			index++;
		}
	}
	return index;
}
void Dating::readPersonsFromFile(string fileName)
{
	string str, strRow, tempName, tempGender;
	vector<string> tempInterest;
	ifstream fin;
	char c;
	try
	{
		fin.open(fileName);
		while (getline(fin, strRow))
		{
			istringstream iss(strRow);
			getline(iss, str, '[');
			getline(iss, str, ',');
			tempGender = str;
			getline(iss, str, ',');
			tempName = str;
			while (getline(iss, str, ','))
			{
				if (str.compare("]") != 0)
					//*p.addInterest(str);
					tempInterest.push_back(str);
			}
			Person p(tempGender, tempName, tempInterest);
			if (tempGender == "Girl")
				addGirlToList(p);
			else if (tempGender == "Boy")
				addBoyToList(p);
		}
	}
	catch (exception e)
	{
		cerr << "Exception: " << fileName << endl;
		cerr << e.what() << endl;
	}
}
void Dating::printLists()
{
	cout << "BoysList: " << boyList.forwardListSize() << endl;
	for (auto boy : boyList)
	{
		cout << boy.value << endl;
	}
	cout << endl << "GirlsList: " << girlList.forwardListSize() << endl;
	for (auto girl : girlList)
	{
		cout << girl.value << endl;
	}
}
void Dating::addBoyToList(Person boy)
{
	boyList.push_front(boy);
}
void Dating::addGirlToList(Person girl)
{
	girlList.push_front(girl);
}